package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import common.Constants;
import common.EarthSimSettings;

public class SimulationControlPanel extends JPanel {
  private static final long serialVersionUID = 9035832612961590803L;
  
  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private JSpinner simTimeStep;
  private JSpinner gridSpacing;
  private JButton btnDrawGrid;  
  private final EarthPanel pnlEarth;
  
  public SimulationControlPanel(EarthPanel pnlEarth) {
    super();
    this.pnlEarth = pnlEarth;
    
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setBorder(BorderFactory.createTitledBorder("Simulation Settings"));
    setAlignmentX(Component.LEFT_ALIGNMENT);
    
    Dimension dim = new Dimension(260,90);
    setPreferredSize(dim);
    setMaximumSize(dim);
    
    add(getGridSpacingControl());
    add(Box.createVerticalStrut(10));
    add(getTimeStepControl());
  }
  
  private JPanel getGridSpacingControl() {
    gridSpacing = new JSpinner(
    new SpinnerNumberModel(Constants.DEFAULT_GRID_SPACING, 
                           Constants.MIN_GRID_SPACING, 
                           Constants.MAX_GRID_SPACING, 1));
    gridSpacing.setPreferredSize(new Dimension(75, 20));
    gridSpacing.setMaximumSize(new Dimension(75, 20));
    gridSpacing.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        settings.setGridSpacing(Integer.parseInt(gridSpacing.getValue().toString()));
      };
    });

    btnDrawGrid = new JButton("Draw Grid");
    btnDrawGrid.setPreferredSize(new Dimension(100, 20));
    btnDrawGrid.setMaximumSize(new Dimension(100, 20));
    btnDrawGrid.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       pnlEarth.drawGrid(settings.getGridSpacing());       
      }
    });
    
    JPanel pnlGrid = new JPanel();
    pnlGrid.setLayout(new BoxLayout(pnlGrid, BoxLayout.LINE_AXIS));
    pnlGrid.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnlGrid.add(new JLabel("Grid Spacing: "));
    pnlGrid.add(gridSpacing);
    pnlGrid.add(btnDrawGrid);
    return pnlGrid;
  }
  
  private JPanel getTimeStepControl() {
    simTimeStep = new JSpinner(
    new SpinnerNumberModel(Constants.DEFAULT_SIM_TIME_STEP, 
                           Constants.MIN_SIM_TIME_STEP, 
                           Constants.MAX_SIM_TIME_STEP, 1));
    simTimeStep.setPreferredSize(new Dimension(75, 25));
    simTimeStep.setMaximumSize(new Dimension(75, 25));
    simTimeStep.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSpinner spn = (JSpinner) e.getSource();
        settings.setTimeStep(Integer.parseInt(spn.getValue().toString()));
      };
    });

    JPanel pnlSimTime = new JPanel();
    pnlSimTime.setLayout(new BoxLayout(pnlSimTime, BoxLayout.LINE_AXIS));
    pnlSimTime.setAlignmentX(Component.LEFT_ALIGNMENT);
    pnlSimTime.add(new JLabel("Simulation Time Step: "));
    pnlSimTime.add(simTimeStep);
    pnlSimTime.add(new JLabel(" (minutes)"));
    return pnlSimTime;
  }
  
  public void disableControls() {
    simTimeStep.setEnabled(false);
    gridSpacing.setEnabled(false);
    btnDrawGrid.setEnabled(false);
  }
  
  public void enableControls() {
    simTimeStep.setEnabled(true);
    gridSpacing.setEnabled(true);
    btnDrawGrid.setEnabled(true);
  }
}
